﻿
/* 
 * Since 2008 - 2012
 */
package com.hh.bss.auth.controller;

import static com.hh.bss.util.extjs.JsonHelper.outJson;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.filters.MappingPropertyFilter;
import net.sf.json.util.PropertyFilter;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;


import com.hh.bss.auth.dao.*;
import com.hh.bss.auth.entity.*;
import com.hh.bss.auth.service.*;
import com.hh.bss.auth.vo.query.*;
import com.hh.bss.base.*;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.common.tree.CompanyCvTreeNode;
import com.hh.bss.sysadmin.entity.SystemDic;
import com.hh.bss.talent.entity.CvContactDetail;
import com.hh.bss.util.*;
import com.hh.bss.util.extjs.ExtJsPageHelper;
import com.hh.bss.util.extjs.JsonHelper;
import com.hh.bss.util.extjs.ListRange;

import java.util.*;





@Controller
public class SystemBranchCompanyController extends BaseSpringController{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	private SystemBranchCompanyService systemBranchCompanyService;
	private static final ObjectMapper objectMapper = new ObjectMapper();
	private SystemBranchCompany systemBranchCompany;
	java.lang.Integer id = null;
	private String[] items;

	
	
	/** 通过spring自动注入 */
	public void setSystemBranchCompanyServiceImpl(SystemBranchCompanyService service) {
		this.systemBranchCompanyService = service;
	}	
	
	
	
	public void setId(java.lang.Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}


	public void get(HttpServletRequest request,HttpServletResponse response,SystemBranchCompanyQuery systemBranchCompanyQuery) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			SystemBranchCompany oldSystemBranchCompany = (SystemBranchCompany)systemBranchCompanyService.getById(systemBranchCompanyQuery.getId());
                         
			if(oldSystemBranchCompany!=null) result.put("data", oldSystemBranchCompany);
			result.put("success", true);
			result.put("msg", "成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	/**
	 * ExtGrid使用
	 * 列表
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response,SystemBranchCompanyQuery systemBranchCompanyQuery) throws IOException
	{
		PageRequest<Map> pr = ExtJsPageHelper.createPageRequestForExtJs(request, DEFAULT_SORT_COLUMNS);
		Page page = systemBranchCompanyService.findPage(systemBranchCompanyQuery);
		
		List<SystemBranchCompany> SystemBranchCompanylist = (List) page.getResult();
		ListRange<SystemBranchCompany> resultList = new ListRange<SystemBranchCompany>();
		resultList.setRows(SystemBranchCompanylist);
		resultList.setTotal(page.getTotalCount());
		resultList.setMessage("ok");
		resultList.setSuccess(true);
		outJson(response,resultList);
	}
	 public String managerPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
			
			return "forward:/jsp/auth/systemBranchCompany/companyOrg.jsp";
			
    }	
	public void listbranchcompany(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		JsonConfig jsonConfig = new JsonConfig();    
		Map mf=new HashMap();
		   mf.put(SystemDic.class, new PropertyFilter() {
			    public boolean apply(Object source, String name, Object value) {
			    //name--map里key的字符，或是bean里的属性
			    //map里的value,或是bean里的属性的值
			    //如果上层的数据给拦截了，那底层的数据将不能被转换
			    //source 可能是hash或bean等	
			     Set set=new HashSet();
			     set.add("id");
			     set.add("name");
			     set.add("branchNbr");			     
			     //set.add("category");
			     if (SystemBranchCompany.class.equals(source.getClass())&&! set.contains(name)){
			    	 return true;
			     }	 
			     return false;
			    }
			   });
		   jsonConfig.setJsonPropertyFilter(new MappingPropertyFilter(mf) {
			   protected boolean keyMatches( Object key, Object source, String name, Object value ) {			   
				     return true;
				   } 
				   });
		   List l=systemBranchCompanyService.findAll();
		   String jsonStr=JSONArray.fromObject(l,jsonConfig).toString();
		   JsonHelper.outJsonString(response,jsonStr);
		
	}

	public void companyorg(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		
		String companyId=request.getParameter("companyId");
		
		try{
		//Object[] os={StringHelper.parseInteger(companyId, 0)};
		//List nodeLists=systemBranchCompanyService.getOrg(StringHelper.parseInteger(companyId, 0));
			List nodeLists=systemBranchCompanyService.getOrg();	
		JsonHelper.outJsonArray(response, nodeLists);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	public void companyorgtreesave(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		
		Map<String, Object> result = new HashMap<String, Object>();
		
		String companyOrgTree=request.getParameter("companyOrgTree");
		//List<CvContactDetail> contact_list = objectMapper.readValue( companyOrgTree, new TypeReference<List<CompanyCvTreeNode>>(){});
		//System.out.println(contact_list);
		try{
		//Object[] os={StringHelper.parseInteger(companyId, 0)};
		//List nodeLists=systemBranchCompanyService.getOrg(StringHelper.parseInteger(companyId, 0));
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			com.hh.bss.auth.client.OnlineUser onlineUser =null;
			if (onlineManager!=null)
			onlineUser=onlineManager.getOnlineUser();
			 Map classMap = new HashMap();
	         classMap.put("children", CompanyCvTreeNode.class);
			CompanyCvTreeNode root= (CompanyCvTreeNode)JSONObject.toBean(JSONObject.fromObject(companyOrgTree),CompanyCvTreeNode.class,classMap);
			systemBranchCompanyService.saveOrgTree(onlineUser,root);	
		
			result.put("success", true);
			result.put("msg", "修改成功!");
		}catch(Exception ex){
			ex.printStackTrace();
		}
		outJson(response,result);
	}
	/**
	 * extGrid保存
	 * @throws IOException
	 */
	public void save(HttpServletRequest request,HttpServletResponse response,SystemBranchCompany systemBranchCompany) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        //设置创建用户和创建时间
			com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchCompany.class,"creator")) {
						try{com.hh.bss.util.BeanUtils.setProperty(systemBranchCompany,"creator",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchCompany.class,"createDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(systemBranchCompany,"createDt",new Date());}catch(Exception ex){}
				}

			systemBranchCompanyService.save(systemBranchCompany);
			result.put("success", true);
			result.put("msg", "添加成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid修改
	 * @throws IOException
	 */
	public void update(HttpServletRequest request,HttpServletResponse response,SystemBranchCompany systemBranchCompany) throws IOException
	{
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
                        SystemBranchCompany oldsystemBranchCompany = (SystemBranchCompany)systemBranchCompanyService.getById(systemBranchCompany.getId());
                         
			bind(request,oldsystemBranchCompany);
                        //设置修改用户和修改时间 
                        com.hh.bss.auth.client.OnlineManager onlineManager=super.getOnlineManager(request, response);
			if (onlineManager!=null){
					com.hh.bss.auth.client.OnlineUser onlineUser=onlineManager.getOnlineUser();
					if (onlineUser!=null&&com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchCompany.class,"modifier")) {
						try{com.hh.bss.util.BeanUtils.setProperty(oldsystemBranchCompany,"modifier",onlineUser.getUsername());}catch(Exception ex){}
					}
				}
							
			if (com.hh.bss.util.BeanUtils.isExistsProperty(SystemBranchCompany.class,"modifiedDt")) {
					try{com.hh.bss.util.BeanUtils.setProperty(oldsystemBranchCompany,"modifiedDt",new Date());}catch(Exception ex){}
				}


			systemBranchCompanyService.update(oldsystemBranchCompany);
			result.put("success", true);
			result.put("msg", "修改成功!");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
	/**
	 * extGrid删除
	 * @throws IOException
	 */
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String ids = request.getParameter("ids");
		String[] idarray = ids.split(",");
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			for (int i = 0; i < idarray.length; i++)
			{
				java.lang.Integer id = new java.lang.Integer((String)idarray[i]);
				systemBranchCompanyService.removeById(id);
			}
			result.put("success", true);
			result.put("msg", "删除成功");
		}
		catch (Exception e)
		{
			result.put("failure", true);
			result.put("msg", e.getMessage());
			e.printStackTrace();
		}
		outJson(response,result);
	}
	
}
